﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sinacor.Infra.Tools.ServiceRegistration.Business
{
    public class Attribute
    {
        #region Attributes

        private Data.Attribute _resourceAccess;
        private Business.Type _businessType;
        private object locker = new object();

        #endregion

        #region Constructor

        public Attribute()
        {
            this._resourceAccess = new Data.Attribute();
            this._businessType = new Business.Type();
        }

        #endregion

        #region Methods

        public List<Entity.Attribute> SelectAttribute(Entity.Attribute entity)
        {
            return _resourceAccess.SelectAttribute(entity, true);
        }

        public Entity.Attribute PersistAttribute(Entity.Attribute entity)
        {
            Entity.Attribute attribute = new Entity.Attribute() { AttributeName = entity.AttributeName, ParentTypeId = entity.ParentTypeId }; ;
            List<Entity.Attribute> attributes = null;

            attributes = _resourceAccess.SelectAttribute(attribute, false);

            if (attributes.Count == 0)
            {
                attribute = InsertAttribute(entity);
            }
            else
            {
                attribute = attributes[0];

                if (!string.IsNullOrEmpty(entity.Description) && (attribute.Description != entity.Description))
                    attribute.Description = entity.Description;
                
                attribute.Type = entity.Type;
                attribute.DateChange = entity.DateChange;
                attribute.DataOrigin = entity.DataOrigin;
                UpdateAttribute(attribute);
            }

            return attribute;
        }

        public Entity.Attribute InsertAttribute(Entity.Attribute entity)
        {
            if (!entity.Type.TypeId.HasValue)
            {
                if (!string.IsNullOrEmpty(entity.Type.FullTypeName))
                    entity.Type = _businessType.PersistType(entity.Type);
                else
                {
                    entity.Type.FullTypeName = "System.Void";
                    entity.Type = _businessType.PersistType(entity.Type);
                }
            }

            entity.AttributeId = this.GetNextAttributeId();
            return _resourceAccess.InsertAttribute(entity);
        }

        public void UpdateAttribute(Entity.Attribute entity)
        {
            if (!entity.Type.TypeId.HasValue)
            {
                if (string.IsNullOrEmpty(entity.Type.FullTypeName))
                    entity.Type.FullTypeName = "System.Void";

                entity.Type = _businessType.PersistType(entity.Type);
            }

            _resourceAccess.UpdateAttribute(entity);
        }

        public void DeleteAttribute(Entity.Attribute entity)
        {
            _resourceAccess.DeleteAttribute(entity);
        }
        
        public int GetNextAttributeId()
        {
            int nextId = 0;

            lock (locker)
            {
                nextId = _resourceAccess.GetNextAttributeId();
            }

            return nextId;
        }

        #endregion
    }
}
